import {createRouter, createWebHashHistory} from "vue-router";
import {getToken} from "@/store/local";

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            name: '404',
            path: '/404',
            component: () => import('@/views/404.vue')
        },
        {
            name: 'Upload',
            path: '/upload',
            component: () => import('@/views/upload.vue')
        },
        {
            name: 'Login',
            path: '/login',
            component: () => import('@/views/login.vue')
        },
        {
            path: '/',
            component: () => import('@/views/index.vue'),
            redirect: '/home',
            children: [
                {
                    name: 'Home',
                    path: '/home',
                    component: () => import('@/views/home.vue')
                },
                {
                    name: 'Goods-List',
                    path: '/goods/list',
                    component: () => import('@/views/goods/list.vue')
                },
                {
                    name: 'Goods-Add',
                    path: '/goods/add',
                    component: () => import('@/views/goods/add.vue')
                },
                {
                    name: 'Goods-Edit',
                    path: '/goods/edit',
                    component: () => import('@/views/goods/edit.vue')
                },
                {
                    name: 'Media-Manage',
                    path: '/store/media',
                    component: () => import('@/views/store/media.vue')
                },
                {
                    name: 'Admin-Manage',
                    path: '/admin/manage',
                    component: () => import('@/views/admin/manage.vue')
                },
                {
                    name: 'Admin-Dict',
                    path: '/admin/dict',
                    component: () => import('@/views/admin/dictionary.vue')
                },
                {
                    name: 'Admin-Resource',
                    path: '/admin/resource',
                    component: () => import('@/views/admin/resource.vue')
                }
            ]
        },

    ]
})

router.beforeEach((to, from, next) => {
    const token = getToken()
    if (!token && to.path !== '/login') {
        next({path: '/login'})
        return
    }
    if (token && to.path === '/login') {
        next({path: '/home'})
        return
    }
    next()
})
export default router